/* scrogui.cxx */

/*
 * 0 Label
 * 1 Button
 * 2
 * 3 Input (alphadec)
 * 4 Input (numeric)
 * 5 image
 * 6 image button
 *
 */


#include "scrogui.hxx"

scrogui :: scrogui (SDL_Rect * newpane, SDL_Surface * target, char * title)
{
	int h = 0;
	gui_pane = newpane;
	gui_surface = target;
	first_element = NULL;
	tab_position = 0;
	gui_title = title;
	gui_color.r = 255;
	gui_color.g = 255;
	gui_color.b = 255;
	gui_highlight_color.r = 255;
	gui_highlight_color.g = 240;
	gui_highlight_color.b = 0;
	pane_color.r = 0;
	pane_color.g = 0;
	pane_color.b = 170;
	button_color.r = 0;
	button_color.g = 0;
	button_color.b = 130;
	pane_border_color.r = 0;
	pane_border_color.g = 0;
	pane_border_color.b = 255;
	pane_title_bg_color.r = 100;
	pane_title_bg_color.g = 100;
	pane_title_bg_color.b = 255;
	gui_alpha = 255;
	blank_screen = 1;

	gui_font = TTF_OpenFont(SCROGUI_DEFAULT_FONT, SCROGUI_DEFAULT_FONTSIZE);
	
	
	if (!gui_font)
	{
		printf("error loading font...\n");
//		return NULL;
	} else
	{
		if (strlen(gui_title) > 0)
		{
			TTF_SizeText(gui_font, gui_title, NULL, &h);
			gui_title_bar_height = h;
		} else
		{
			gui_title_bar_height = 0;
		}
	}
}

scrogui :: ~scrogui()
{
	gui_element * element_pointer;
	gui_element * temp_pointer;
	unsigned int counter = 1;
	unsigned int counter2 = 1;
	element_pointer = first_element;
	while(element_pointer->next_element != NULL)
	{
		temp_pointer = element_pointer->next_element;
		element_pointer = temp_pointer;
		counter++;
	}
	printf("freeing gui...you have %d elements...", counter);
	
	while(first_element != NULL)
	{
		element_pointer = first_element;
		counter2 = 1;
		while(counter2 < counter)
		{
			temp_pointer = element_pointer->next_element;
			element_pointer = temp_pointer;
			counter2++;
		}
		if (counter > 1)
		{
			delete(element_pointer->next_element);
		} else
		{
			delete(element_pointer);
			first_element = NULL;
		}
		counter--;
	}
	printf("done...\n");
	TTF_CloseFont(gui_font);
}


unsigned int scrogui :: addLabel(const char * el_name, char * string, unsigned int x_offset, unsigned int y_offset)
{
	return addElement(el_name, 0, string, NULL, x_offset, y_offset, NULL, 0);
}

unsigned int scrogui :: addButton(const char * el_name, char * string, unsigned int x_offset, unsigned int y_offset, unsigned int(*function)(unsigned int))
{
	return addElement(el_name, 1, string, NULL, x_offset, y_offset, function, 0);
}

unsigned int scrogui :: addInput(const char * el_name, char * string, char * value, unsigned int x_offset, unsigned int y_offset, unsigned int max_length, unsigned int type)
{
	if ((type >= 0)  && (type < 2))
	{
		return addElement(el_name, 3+type, string, value, x_offset, y_offset, NULL, max_length);
	} else
	{
		return addElement(el_name, 3, string, value, x_offset, y_offset, NULL, max_length);
	}
}

unsigned int scrogui :: addImage(const char * el_name, SDL_Surface * image, unsigned int x_offset, unsigned int y_offset)
{
	return addElement(el_name, 5, image, x_offset, y_offset, NULL);
}

unsigned int scrogui :: addImageButton(const char * el_name, SDL_Surface * image, unsigned int x_offset, unsigned int y_offset, unsigned int(*function)(unsigned int))
{
	return addElement(el_name, 6, image, x_offset, y_offset, function);
}

unsigned int scrogui :: addElement(const char * el_name, unsigned int type, SDL_Surface * picture, unsigned int x_offset, unsigned int y_offset, unsigned int(*function)(unsigned int))
{
	gui_element * temp_pointer;
    gui_element * element_pointer;
	unsigned int counter = 1;

	if (first_element != NULL)
	{
		element_pointer = first_element;
		while(element_pointer->next_element != NULL)
		{
			temp_pointer = element_pointer->next_element;
			element_pointer = temp_pointer;
			counter++;
		}
	}
    temp_pointer = new gui_element(el_name, type, picture, x_offset + gui_pane->x, y_offset + gui_pane->y + gui_title_bar_height, counter, pane_color, function);
    if (first_element == NULL)
    {
        first_element = temp_pointer;
    } else
    {
        element_pointer->next_element = temp_pointer;
    }
}

unsigned int scrogui :: addElement(const char * el_name, unsigned int type, char * string, char * value, unsigned int x_offset, unsigned int y_offset, unsigned int(*function)(unsigned int), unsigned int max_length)
{
	gui_element * temp_pointer;
    gui_element * element_pointer;
	unsigned int counter = 1;

	if (first_element != NULL)
	{
		element_pointer = first_element;
		while(element_pointer->next_element != NULL)
		{
			temp_pointer = element_pointer->next_element;
			element_pointer = temp_pointer;
			counter++;
		}
	}
    temp_pointer = new gui_element(el_name, type, string, value, x_offset + gui_pane->x, y_offset + gui_pane->y + gui_title_bar_height, counter, gui_font, pane_color, gui_color, function, max_length);
    if (first_element == NULL)
    {
        first_element = temp_pointer;
    } else
    {
        element_pointer->next_element = temp_pointer;
    }
}

unsigned int scrogui :: message_box(char * title, char * string, unsigned int type)
{
    /* type can be:
     *
     * 0 One Button: "OK"
     * 1 two buttons: "Yes"/"No"
     */
    SDL_Rect dialog_pane;
    int w = 0;
    int offset = 0;
    unsigned int quit = 0;
    unsigned int ret = 1;
    TTF_SizeText(gui_font, string, &w, NULL);
    
    dialog_pane.w = w+20;
    if (dialog_pane.w < 200) dialog_pane.w = 200;
    dialog_pane.h = 150;
    dialog_pane.x = XMAX / 2 - dialog_pane.w / 2;
    dialog_pane.y = YMAX / 2 - dialog_pane.h / 2 - YMAX / 10;

    scrogui * dialog = new scrogui(&dialog_pane, gui_surface, title);
    dialog->addLabel("label1", string, 10, 10);
    dialog->setBlank(0);
    if (type == 0)
    {
		TTF_SizeText(gui_font, "Confirm", &offset, NULL);
		dialog->addButton((char*)"button1", "Confirm", dialog_pane.w - offset - 10, dialog_pane.h - 40, NULL);
    }
    if (type == 1)
    {
		TTF_SizeText(gui_font, "Yes, please", &offset, NULL);
		dialog->addButton("button1", "Yes, please", dialog_pane.w - offset - 10, dialog_pane.h - 40, NULL);
		dialog->addButton("button2", "No, thanks", 10, dialog_pane.h - 40, NULL);
    }
    fade_sdl_surface(gui_surface, SCROGUI_DEFAULT_FADE);
    dialog->redrawGUI(NULL);
    SDL_Flip(gui_surface);
    while(!quit)
    {
		if (SDL_PollEvent(&event))
		{
			dialog->guiEventHandler(&event);
			dialog->redrawGUI(NULL);
			SDL_Flip(screen);
		}
		if (dialog->get_flags_of_object("button1")) quit = 1;
		if (dialog->get_flags_of_object("button2")) 
		{
		    quit = 1;
		    ret = 0;
		}
		SDL_Delay(TimeLeft());
    }
    delete(dialog);
    return ret;
}

/* update an image object with another image */
unsigned int scrogui :: updateImage(const char * name, SDL_Surface * image)
{
    unsigned int ret = 0;
    unsigned int quit = 0;
    gui_element * element_pointer;
    if (first_element != NULL)
    {
		element_pointer = first_element;
		while((element_pointer != NULL) && (quit == 0))
		{
			if (strcmp(element_pointer->name, name) == 0)
			{
			    SDL_BlitSurface(image, NULL, element_pointer->rendered_element, NULL);
			    quit = 1;
			    ret = 1;
			}
			element_pointer = element_pointer->next_element;
		}
    }
    return ret;
}

/* update string of a button */
unsigned int scrogui :: updateString(const char * el_name, char * new_string)
{
	unsigned int ret = 0;
	unsigned int quit = 0;
	int w = 0, h = 0;
	SDL_Surface * temp_surface;
	SDL_Rect position;
	gui_element * element_pointer;
	gui_element * temp_pointer;
	if (first_element != NULL)
    {
		element_pointer = first_element;
		while((element_pointer != NULL) && (quit == 0))
		{
			if (strcmp(element_pointer->name, el_name) == 0)
			{
				element_pointer->label = new_string;
			    /* re-render string */
			    TTF_SizeText(gui_font, new_string, &w, &h);
			    position.x = 0;
			    position.y = 0;
				position.w = w;
				position.h = h;
				SDL_FreeSurface(element_pointer->rendered_element);
				element_pointer->rendered_element = SDL_CreateRGBSurface(SCROGUI_VIDEO_MODE, position.w+1, position.h, 16, 0, 0, 0, 0);
				SDL_FillRect(element_pointer->rendered_element, NULL, SDL_MapRGB(element_pointer->rendered_element->format, pane_color.r,pane_color.g,pane_color.b));
				temp_surface = TTF_RenderText_Blended(gui_font, new_string, gui_color);
				//	rendered_element = SDL_DisplayFormat(temp_surface);
				SDL_BlitSurface(temp_surface, NULL, element_pointer->rendered_element, NULL);

		    	SDL_FreeSurface(temp_surface);
			    quit = 1;
			    ret = 1;
			}
			temp_pointer = element_pointer->next_element;
			element_pointer = temp_pointer;
		}
    }
	return ret;
}


/* update value of a input field */
unsigned int scrogui :: updateValue(const char * el_name, char * new_string)
{
	unsigned int ret = 0;
	gui_element * element_pointer;
	if (first_element != NULL)
    {
		element_pointer = first_element;
		while((element_pointer != NULL) && (ret == 0))
		{
			if (strcmp(element_pointer->name, el_name) == 0)
			{
				changeInputValue(element_pointer, new_string);
				ret = 1;
			}
			element_pointer = element_pointer->next_element;
		}
	}
	return ret;
}

/* return value of a input field */
char * scrogui :: returnValue(const char * el_name)
{
	char * ret = new char [50];
	gui_element * element_pointer;
	if (first_element != NULL)
    {
		element_pointer = first_element;
		while(element_pointer != NULL)
		{
			if (strcmp(element_pointer->name, el_name) == 0)
			{
				sprintf(ret, "%s", element_pointer->value);
			}
			element_pointer = element_pointer->next_element;
		}
	}
	return ret;
}

/* redraw the pane */
unsigned int scrogui :: redrawPane(SDL_Surface * spec_target)
{
	SDL_Surface * target;
	SDL_Surface * title_surface;
	SDL_Rect destination;
	int w = 0, h = 0;
	destination.x = 2 + gui_pane->x;
	destination.y = 2 + gui_pane->y;
	if (spec_target == NULL)
	{
		target = gui_surface;
	} else
	{
		target = spec_target;
	}
	boxRGBA(target, gui_pane->x+1, gui_pane->y+1, gui_pane->x+gui_pane->w-1, gui_pane->y+gui_pane->h-1, pane_color.r, pane_color.g, pane_color.b, gui_alpha);
	rectangleRGBA(target, gui_pane->x, gui_pane->y, gui_pane->x+gui_pane->w, gui_pane->y+gui_pane->h, pane_border_color.r, pane_border_color.g, pane_border_color.b, gui_alpha);
//	title_surface = TTF_RenderText_Blended(gui_font, gui_title, gui_color);
	if (strlen(gui_title) > 0)
	{
		TTF_SizeText(gui_font, gui_title, &w, &h);
		destination.w = w;
		destination.h = h;
		rectangleRGBA(target, gui_pane->x, gui_pane->y, gui_pane->x+gui_pane->w, gui_pane->y+destination.h + 2, pane_border_color.r, pane_border_color.g, pane_border_color.b, gui_alpha);
		boxRGBA(target, gui_pane->x + 1, gui_pane->y + 1, gui_pane->x + gui_pane->w - 1, gui_pane->y + destination.h + 1, pane_title_bg_color.r, pane_title_bg_color.g, pane_title_bg_color.b, gui_alpha);
		title_surface = TTF_RenderText_Blended(gui_font, gui_title, gui_color);
		SDL_BlitSurface(title_surface, NULL, target, &destination);
		SDL_FreeSurface(title_surface);
	}
    return 1;
}

unsigned int scrogui :: setAlpha(unsigned int alpha)
{
	gui_alpha = alpha;
	return 1;
}

unsigned int scrogui :: setBlank(unsigned int blank)
{
	blank_screen = blank;
	return 1;
}

unsigned int scrogui :: redrawGUI(SDL_Surface * spec_target)
{
	gui_element * element_pointer;
//	gui_element * temp_pointer;
	unsigned int counter = 1;
	unsigned int quit = 0;
	element_pointer = first_element;
	SDL_Surface * target;
	
	SDL_Rect destination;
	if (spec_target == NULL)
	{
		target = gui_surface;
	} else
	{
		target = spec_target;
	}

	if (element_pointer != NULL)
	{
		SDL_Surface *blank = SDL_CreateRGBSurface(SCROGUI_VIDEO_MODE, 800, 600, 16, 0, 0, 0, 0);
		if (blank_screen == 1)
		{
			SDL_BlitSurface(blank, NULL, target, NULL);
		}
		redrawPane(target);
		while (!quit)
		{
			destination.x = element_pointer->position.x;
			destination.y = element_pointer->position.y;
			SDL_BlitSurface(element_pointer->rendered_element, NULL, target, &destination);
			if ((element_pointer->type == 1) || (element_pointer->type == 3) || (element_pointer->type == 4) || (element_pointer->type == 6))
			{
				rectangleRGBA(target, element_pointer->position.x - 1, element_pointer->position.y, element_pointer->position.x + element_pointer->position.w + 1, element_pointer->position.y + element_pointer->position.h, button_color.r, button_color.g, button_color.b, gui_alpha);
				if (tab_position == counter)
				{
					highlightElement(&element_pointer->position);
				}
			} else
			{
				if (tab_position == counter) tab_position++;
			}
			
			if (tab_position != counter)
			{
				if ((element_pointer->type != 0) && (element_pointer->type != 5)) unHighlightElement(&element_pointer->position);
			}
			
			if (element_pointer->next_element != NULL)
			{
				element_pointer = element_pointer->next_element;
//				element_pointer = temp_pointer;
				counter++;
			} else
			{
				quit = 1;
			}
		}
		if (tab_position > counter) tab_position = 1;
		SDL_FreeSurface(blank);
	}
	return 1;
}

SDL_Surface * scrogui :: returnDrawnSurface(void)
{
	SDL_Surface * return_surface;
	redrawGUI(return_surface);
	return return_surface;
}

unsigned int scrogui :: highlightElement(SDL_Rect * highlight_rect)
{
	rectangleRGBA(gui_surface, highlight_rect->x - 3, highlight_rect->y - 1, highlight_rect->x + highlight_rect->w + 3, highlight_rect->y + highlight_rect->h + 1, gui_highlight_color.r, gui_highlight_color.g, gui_highlight_color.b, 255);
	return 1;
}

unsigned int scrogui :: unHighlightElement(SDL_Rect * highlight_rect)
{
	rectangleRGBA(gui_surface, highlight_rect->x - 3, highlight_rect->y - 1, highlight_rect->x + highlight_rect->w + 3, highlight_rect->y + highlight_rect->h + 1, pane_color.r, pane_color.g, pane_color.b, 255);
	return 1;
}

unsigned int scrogui :: guiEventHandler(SDL_Event * gui_event)
{
	unsigned int ret = 0;
	gui_element * element_pointer;
//	gui_element * temp_pointer;
	unsigned int counter = 1;
	unsigned int quit = 0;

	if (gui_event->type == SDL_KEYDOWN)
	{
		switch(gui_event->key.keysym.sym)
		{
			case SDLK_TAB:
					tab_position++;
			break;
			
			case SDLK_RETURN:
				element_pointer = first_element;
				while(element_pointer !=  NULL)
				{
					if (tab_position == counter)
					{
						if ((element_pointer->type == 1) || (element_pointer->type == 6))
						{
							if (element_pointer->button_callback != NULL)
							{
							    element_pointer->button_callback(1);
							}
						} else if ((element_pointer->type == 3) || (element_pointer->type == 4))
						{
							handleInput(element_pointer);
						}
						element_pointer->flags |= SCROGUI_BUTTON_PRESSED;
					}					
					element_pointer = element_pointer->next_element;
//					element_pointer = temp_pointer;
					counter++;
				}
			break;
		}
	} else if (gui_event->type == SDL_MOUSEBUTTONDOWN)
	{
		element_pointer = first_element;
		while((element_pointer!= NULL) && (!quit))
		{
			if ((element_pointer->type == 1) || (element_pointer->type == 3) || (element_pointer->type == 4) || (element_pointer->type == 6))
			{
				if (gui_event->motion.y > element_pointer->position.y)
				{
					if (gui_event->motion.y < (element_pointer->position.y + element_pointer->position.h))
					{
						if (gui_event->motion.x > element_pointer->position.x)
						{
							if (gui_event->motion.x < (element_pointer->position.x + element_pointer->position.w))
							{
								tab_position = counter;
								quit = 1;
							}
						}
					}
				}
			}
			element_pointer = element_pointer->next_element;
//			element_pointer = temp_pointer;
			counter++;
		}
	} else if (gui_event->type == SDL_MOUSEBUTTONUP)
	{
        element_pointer = first_element;
		while((element_pointer!= NULL) && (!quit))
		{
			if ((element_pointer->type == 1) || (element_pointer->type == 3) || (element_pointer->type == 4) || (element_pointer->type == 6))
			{
				if ((counter == tab_position) && (gui_event->button.button == SDL_BUTTON_LEFT))
				{
					if (gui_event->motion.y > element_pointer->position.y)
					{
						if (gui_event->motion.y < (element_pointer->position.y + element_pointer->position.h))
						{
							if (gui_event->motion.x > element_pointer->position.x)
							{
								if (gui_event->motion.x < (element_pointer->position.x + element_pointer->position.w))
								{
								    if ((element_pointer->type == 3) || (element_pointer->type == 4))
								    {
										handleInput(element_pointer);
								    } else if (element_pointer->button_callback != NULL)
								    {
										element_pointer->button_callback(1);
								    }
								    element_pointer->flags |= SCROGUI_BUTTON_PRESSED;
								    quit = 1;
								}
							}
						}
					}
					//tab_position = counter;
				}
			}
			element_pointer = element_pointer->next_element;
//			element_pointer = temp_pointer;
			counter++;
		}
	}
	return ret;
}

unsigned int scrogui :: redrawHandleInput(gui_element * input_element, char * temp_string)
{
	SDL_Surface * temp_surface;
	SDL_Surface * temp_surface2;
	SDL_Rect destination;
	int x = 0, y = 0, w = 0, h = 0;
	TTF_SizeText(gui_font, input_element->label, &x, &y);
	destination.x = x;
	destination.y = y;
	TTF_SizeText(gui_font, temp_string, &w, &h);
	destination.w = w;
	destination.h = h;
	destination.x += input_element->position.x;
	destination.y = input_element->position.y;
	destination.w += 7;
	temp_surface2 = SDL_CreateRGBSurface(SCROGUI_VIDEO_MODE, destination.w, destination.h, 16, 0, 0, 0, 0);
	TTF_SizeText(gui_font, temp_string, &w, &h);
	destination.w = w;
	destination.h = h;

	SDL_FillRect(temp_surface2, NULL, SDL_MapRGB(temp_surface2->format, pane_color.r,pane_color.g,pane_color.b) );
	temp_surface = TTF_RenderText_Blended(gui_font, temp_string, gui_highlight_color);
	SDL_BlitSurface(temp_surface, NULL, temp_surface2, NULL);
	SDL_BlitSurface(temp_surface2, NULL, gui_surface, &destination);
	SDL_FreeSurface(temp_surface);
	SDL_FreeSurface(temp_surface2);
}

unsigned int scrogui :: changeInputValue(gui_element * input_element, char * temp_string)
{
	unsigned int temp_w = 0;
	SDL_Surface * temp_surface;
	SDL_Rect val_dest;
	int w = 0, h = 0;
	sprintf(input_element->value, "%s", temp_string);
	SDL_FreeSurface(input_element->rendered_element);
	TTF_SizeText(gui_font, input_element->label, &w, &h);
	input_element->position.w = w;
	input_element->position.h = h;
	temp_w = input_element->position.w;
	TTF_SizeText(gui_font, input_element->value, &w, &h);
	input_element->position.w = w;
	input_element->position.h = h;
	temp_w += input_element->position.w;
	input_element->position.w = temp_w;
	input_element->rendered_element = SDL_CreateRGBSurface(SCROGUI_VIDEO_MODE, input_element->position.w, input_element->position.h, 16, 0, 0, 0, 0);
	SDL_FillRect(input_element->rendered_element, NULL, SDL_MapRGB(input_element->rendered_element->format, pane_color.r,pane_color.g,pane_color.b));
	temp_surface = TTF_RenderText_Blended(gui_font, input_element->label, gui_color);
	SDL_BlitSurface(temp_surface, NULL, input_element->rendered_element, NULL);
	TTF_SizeText(gui_font, input_element->label, &w, &h);
	temp_w = w;
	input_element->position.h = h;
	val_dest.x = temp_w;
	val_dest.y = 0;
	temp_surface = TTF_RenderText_Blended(gui_font, input_element->value, gui_color);
	SDL_BlitSurface(temp_surface, NULL, input_element->rendered_element, &val_dest);
}

unsigned int scrogui :: handleInput(gui_element * input_element)
{
	unsigned int quit = 0;
	char * temp_string = new char [255];
	unsigned int j = strlen(input_element->value);
	sprintf(temp_string, "%s<", input_element->value);
	unHighlightElement(&input_element->position);

	rectangleRGBA(gui_surface, input_element->position.x - 1, input_element->position.y, input_element->position.x + input_element->position.w + 1, input_element->position.y + input_element->position.h, pane_color.r, pane_color.g, pane_color.b, 255);

	redrawHandleInput(input_element, temp_string);

	SDL_Flip(gui_surface);
        
    printf("type is %d\n", input_element->type);
        
	while(quit == 0)
	{
		if (SDL_PollEvent(&event))
		{
			if (event.type == SDL_KEYDOWN)
			{
				switch (event.key.keysym.sym)
				{
					case SDLK_BACKSPACE:
						if (j > 0)
						{
							j--;
							*(temp_string + j) = 0;
							sprintf(temp_string, "%s<", temp_string);
							redrawHandleInput(input_element, temp_string);
							SDL_Flip(gui_surface);

							while(event.key.type != SDL_KEYUP)
							{
								SDL_PollEvent(&event);
							}
						}
					break;
					
					case SDLK_RETURN:
						*(temp_string + j) = '\0';
						quit = TRUE;
					break;
					
					case SDLK_ESCAPE:
						quit = 2;
					break;
					
					default:
                        if (((input_element->type == 4) && ((event.key.keysym.sym >= SDLK_0) && (event.key.keysym.sym <= SDLK_9))) ||
                            ((input_element->type == 3) && (((event.key.keysym.sym >= SDLK_0) && (event.key.keysym.sym <= SDLK_9)) ||
                            ((event.key.keysym.sym >= SDLK_a) && (event.key.keysym.sym <= SDLK_z)) ||
                             (event.key.keysym.sym == SDLK_PERIOD) || (event.key.keysym.sym == SDLK_MINUS) ||
                             (event.key.keysym.sym == SDLK_UNDERSCORE))))
						{
							if (j < input_element->max_len)
							{
								*(temp_string + j) = event.key.keysym.sym;
								*(temp_string + j + 1) = '<';
								*(temp_string + j + 2) = '\0';
								j++;
								redrawHandleInput(input_element, temp_string);
								SDL_Flip(gui_surface);
								while(event.key.type != SDL_KEYUP)
								{
									SDL_PollEvent(&event);
								}
							}
						}
					break;
				}
			}
			
		}
		SDL_Delay(TimeLeft());
	}
	if (quit == 1)
	{
		changeInputValue(input_element, temp_string);
	}
	highlightElement(&input_element->position);
}

unsigned int scrogui :: get_flags_of_object(const char * name)
{
    unsigned int ret = 0;
    unsigned int quit = 0;
    gui_element * element_pointer;
//    gui_element * temp_pointer;
    if (first_element != NULL)
    {
		element_pointer = first_element;
		while((element_pointer != NULL) && (quit == 0))
		{
			if (strcmp(element_pointer->name, name) == 0)
			{
			    ret = element_pointer->flags;
			    quit = 1;
			}
	//		temp_pointer = element_pointer->next_element;
	//		element_pointer = temp_pointer;
			element_pointer = element_pointer->next_element;
		}
    }
    if (quit = 0) printf("object '%s' was not found.\n", name);
    return ret;
}

unsigned int scrogui :: set_flags_of_object(const char * name, unsigned int flags_to_set)
{
    unsigned int ret = 0;
    unsigned int quit = 0;
    gui_element * element_pointer;
//    gui_element * temp_pointer;
    if (first_element != NULL)
    {
		element_pointer = first_element;
		while((element_pointer != NULL) && (quit == 0))
		{
			if (strcmp(element_pointer->name, name) == 0)
			{
			    element_pointer->flags = flags_to_set;
			    quit = 1;
			    ret = 1;
			}
//			temp_pointer = element_pointer->next_element;
//			element_pointer = temp_pointer;
			element_pointer = element_pointer->next_element;
		}
    }
    return ret;
}

gui_element :: gui_element(const char * element_name, unsigned int element_type, char * element_string, char * element_value, unsigned int element_x_offset, unsigned int element_y_offset, unsigned int element_order, TTF_Font * font, SDL_Color pane_col, SDL_Color gui_col, unsigned int(*function_pointer)(unsigned int), unsigned int max_length)
{
	char * temp_string;
	unsigned int temp_w = 0;
	int w = 0, h = 0;
	strcpy(name, element_name);
	type = element_type;
	label = element_string;
	value = element_value;
	max_len = max_length;
	SDL_Surface * temp_surface;
	position.x = element_x_offset;
	position.y = element_y_offset;
	position.w = 0;
	position.h = 0;
	flags = 0;
	
	order = element_order;
	button_callback = function_pointer;
	next_element = NULL;
	
	if (value == NULL)
	{
		TTF_SizeText(font, element_string, &w, &h);
		position.w = w;
		position.h = h;
	} else
	{
		TTF_SizeText(font, label, &w, NULL);
		position.w = w;
		position.h = h;
		temp_w = position.w;
		TTF_SizeText(font, value, &w, &h);
		position.h = h;
		temp_w += w;
		position.w = temp_w;
	}
	rendered_element = SDL_CreateRGBSurface(SCROGUI_VIDEO_MODE, position.w+1, position.h, 16, 0, 0, 0, 0);
	SDL_FillRect(rendered_element, NULL, SDL_MapRGB(rendered_element->format, pane_col.r,pane_col.g,pane_col.b));
	temp_surface = TTF_RenderText_Blended(font, label, gui_col);
//	rendered_element = SDL_DisplayFormat(temp_surface);
	SDL_BlitSurface(temp_surface, NULL, rendered_element, NULL);
	if (value != NULL)
	{
//		SDL_BlitSurface(temp_surface, NULL, rendered_element, NULL);
		
		TTF_SizeText(font, label, &w, &h);
		temp_w = w;
		position.h = h;
		SDL_Rect val_dest;
		val_dest.x = temp_w;
		val_dest.y = 0;
		temp_surface = TTF_RenderText_Blended(font, value, gui_col);
		SDL_BlitSurface(temp_surface, NULL, rendered_element, &val_dest);
	}

//	if (value == NULL) SDL_BlitSurface(temp_surface, NULL, rendered_element, NULL);
//	if (value == NULL) rendered_element = SDL_DisplayFormat(temp_surface);
	SDL_FreeSurface(temp_surface);
}

gui_element :: gui_element(const char * element_name, unsigned int element_type, SDL_Surface * image, unsigned int element_x_offset, unsigned int element_y_offset, unsigned int element_order, SDL_Color pane_col, unsigned int(*function_pointer)(unsigned int))
{
	char * temp_string;
	unsigned int temp_w = 0;
	strcpy(name, element_name);
	type = element_type;
	label = '\0';
	value = '\0';
	max_len = 0;
	SDL_Surface * temp_surface;
	position.x = element_x_offset;
	position.y = element_y_offset;
	position.w = image->w;
	position.h = image->h;
	flags = 0;
	
	order = element_order;
	button_callback = function_pointer;
	next_element = NULL;
	
	rendered_element = SDL_CreateRGBSurface(SCROGUI_VIDEO_MODE, position.w, position.h, 16, 0, 0, 0, 0);
	SDL_FillRect(rendered_element, NULL, SDL_MapRGB(rendered_element->format, pane_col.r, pane_col.g, pane_col.b));
	temp_surface = SDL_DisplayFormat(image);
	SDL_BlitSurface(temp_surface, NULL, rendered_element, NULL);
	SDL_FreeSurface(temp_surface);
}

gui_element :: ~gui_element()
{
	SDL_FreeSurface(rendered_element);
}

